package com.example.ttl2.config;


import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;

import java.util.HashMap;
import java.util.Map;


/**
 * ClassName:RabbitConfig
 * Package:com.example.ttl2.config
 * Description:
 *
 * @Author:ZhangXin
 * @Create:2023/8/12-14:59
 * @Version:v1.0
 */
@Configuration
public class RabbitConfig {

    @Value("${my.exchangeName}")
    private String exchangeName;

    @Value("${my.queueName}")
    private String queueName;

    @Bean
    public DirectExchange directExchange() {
        return ExchangeBuilder.directExchange(exchangeName).build();
    }

    @Bean
    public Queue queue() {
        //方式1 new Queue 的方式
        //String name(队列名称),
        //boolean durable(是否持久化),
        //boolean exclusive(是否排他队列),
        //boolean autoDelete(是否自动删除),
        //@Nullable Map<String, Object> arguments(传的参数)

        Map<String, Object> arguments = new HashMap<>();
        //
        arguments.put("x-message-ttl", 15000);
        //设置过期时间
        return new Queue(queueName, true, false, false, arguments);
        //方式2 建造者模式
        //return QueueBuilder.durable(queueName).withArguments(arguments).build();


//        return QueueBuilder.durable(queueName).build();
    }

    @Bean
    public Binding binding(DirectExchange directExchange, Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with("info");
    }

}
